# File: OutputJournal.py
#
# Author: J. Teeuw
#

# import libraries
import time

from OutputController import *


# class definition
class OutputJournal(OutputController):
	
	#####
	# Constructor
	#####
	
	#
	def __init__(self, file):
		'''
			Construct a new Journal Output Controller
			
			@param file: the file to append the log messages to
		'''
		# call super constructor
		OutputController.__init__(self)
		
		# open journal for writing (append)
		self._journal = open(file, 'a')
		
		# write header to journal
		self._journal.write("####### " + time.strftime("%d-%b-%Y @ %H:%M:%S", time.gmtime()) + " #######\n")
	
	#
	def __del__(self):
		'''
			Destruct the Journal Output Controller and close the file handle
		'''
		# write journal ending
		self._journal.write("\n\n")
		
		# flush output buffer
		self._journal.flush()
		
		# close journal
		self._journal.close()
	
	
	#####
	# Controller Specific Procedures
	#####
	
	#
	def LogMessage(self, message):
		'''
			Log a message to the journal (with timestamp)
			
			@param message: the message to log to the journal
		'''
		# write message to journal
		self._journal.write(time.strftime("%d-%b-%Y @ %H:%M:%S", time.gmtime()) + " | " + message + "\n")
		
	
	#####
	# Level 1: Coarse Geom Events
	#####
	
	#
	def GeomAdded(self, geom):
		'''
			Log the GeomAdded event to the journal
			
			@param geom: the geom that triggered this event
			
			@note: this is a private method and should not be called by the user
		'''
		# log event
		self.LogMessage("ADDED: " + geom.__class__.__name__ + " (" + str(geom.GetLabel()) + ", " + str(geom.GetID()) + ")")
	
	
	#
	def GeomRemoved(self, geom):
		'''
			Log the GeomRemoved event to the journal
			
			@param geom: the geom that triggered this event
			
			@note: this is a private method and should not be called by the user
		'''
		# log event
		self.LogMessage("REMOVED: " + geom.__class__.__name__ + " (" + str(geom.GetLabel()) + ", " + str(geom.GetID()) + ")")
	
	
	#####
	# Level 2: More Refined Geom Events
	#####
	
	#
	def GeomPositionUpdated(self, geom):
		'''
			Log the GeomPositionUpdated event to the journal
			
			@param geom: the geom that triggered this event
			
			@note: this is a private method and should not be called by the user
		'''
		# log event
		posx, posy, posz = geom.GetPosition()
		self.LogMessage("POSITION: " + geom.__class__.__name__ + " (" + str(geom.GetLabel()) + ", " + str(geom.GetID()) + "); x=" + str(posx) + ", y=" + str(posy) + ", z=" + str(posz))
		
	
	#
	def GeomScaleUpdated(self, geom):
		'''
			Log the GeomScaleUpdated event to the journal
			
			@param geom: the geom that triggered this event
			
			@note: this is a private method and should not be called by the user
		'''
		# log event
		scalex, scaley, scalez = geom.GetScale()
		self.LogMessage("SCALE: " + geom.__class__.__name__ + " (" + str(geom.GetLabel()) + ", " + str(geom.GetID()) + "); x=" + str(scalex) + ", y=" + str(scaley) + ", z=" + str(scalez))
		
	
	#
	def GeomOrientationUpdated(self, geom):
		'''
			Log the GeomOrientationUpdated event to the journal
			
			@param geom: the geom that triggered this event
			
			@note: this is a private method and should not be called by the user
		'''
		# log event
		orientx, orienty, orientz = geom.GetOrientation()
		self.LogMessage("ORIENTATION: " + geom.__class__.__name__ + " (" + str(geom.GetLabel()) + ", " + str(geom.GetID()) + "); x=" + str(orientx) + ", y=" + str(orienty) + ", z=" + str(orientz))
	